#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <http.h>

/*
 IP ip;
 Date date;
 char method[METHOD_LEN];
 URI uri;
 char protocol[PROTOCOL_LEN];
 unsigned short status;
 //unsigned short status2;
 char referer[REFERER_LEN];
 char ua[UA_LEN];
 */

void test(const char * line) {
	HttpLogEntry entry;
	if (parse_http_log_entry(&entry, line) == 0) {
		printf("IP: \t%u.%u.%u.%u\n", entry.ip.parts.a, entry.ip.parts.b,
				entry.ip.parts.c, entry.ip.parts.d);
		printf("Date: \t%04u-%02u-%02u %02u:%02u:%02u\n", entry.time.year,
				entry.time.month, entry.time.date, entry.time.hour,
				entry.time.minute, entry.time.second);
		printf("Method: \t%s\n", entry.method);
		printf("URI: \t%s\n", entry.uri.path);
		list * pEntries = all_entries(entry.uri.params);
		map_entry * pParamEntry = NULL;
		while ((pParamEntry = next(pEntries)) != NULL) {
			printf("\t\t%s \t=> %s\n", pParamEntry->key, (char *)pParamEntry->pData);
		}
		destroy_list(pEntries);
		printf("\t\t# %s\n", entry.uri.anchor);
		printf("Status: \t%u\n", entry.status);
		printf("Referer: \t%s\n", entry.referer);
		printf("UA:\t%s\n", entry.user_agent);
		discard_http_log_entry(&entry);
	} else {
		printf("Invalid line {{ %s }}, parse_http_log_entry = NULL\n", line);
	}
}

int main() {
	test("");
	test(
			"220.129.123.80 - - [22/Jul/2011:01:00:34 +0800] \"GET /?ac=record&pv=2011072200228787&pi=902204&ct=1311267667974&x=155&y=759&xp=HTML%2FBODY%2FFORM%2FUL%2FLI%5B3%5D%2FDIV%5B2%5D%2FDIV%2FDIV%5B2%5D%2FDIV%5B2%5D%2FDIV%5B2%5D%2FDIV%5B3%5D%2FDIV%5B3%5D%2FDIV%2FINPUT%2F&url=http%3A%2F%2Fhotels.ctrip.com%2FDomestic%2FInputNewOrder.aspx HTTP/1.1\" 200 1 \"http://hotels.ctrip.com/Domestic/InputNewOrder.aspx\" \"Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0\"");
	test(
			"219.82.252.101 - - [22/Jul/2011:01:00:33 +0800] \"GET /test/path.php?ac=record&pv=2011072200229123&pi=103005&ct=1311267826422&x=186&y=564&xp=HTML%2FBODY%2FFORM%2FUL%2FLI%5B3%5D%2FDIV%2FDIV%2FDIV%5B4%5D%2FDIV%5B2%5D%2FTABLE%2FTBODY%2FTR%5B2%5D%2FTD%2FDIV%2FTABLE%2FTBODY%2FTR%5B4%5D%2FTD%2FA%2F&url=http%3A%2F%2Fvacations.ctrip.com%2Fbooking%2FPkgReserveOption.aspx%3FTmpOrderID%3DWIArgFZofxgaSGrvijhBMQ%253d%253d%23 HTTP/1.1\" 200 1 \"http://vacations.ctrip.com/booking/PkgReserveOption.aspx?TmpOrderID=WIArgFZofxgaSGrvijhBMQ%3d%3d\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\"");

	return 0;
}
